package com.etung.iot.demo.controller;

import com.etung.iot.demo.constant.CommonConstant;
import com.etung.iot.demo.services.MqttAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/mqtt")
@Api(tags = "MQTT认证接口")
public class MQTTAuthController {

    @Autowired
    private MqttAuthService mqttAuthService;

    @PostMapping("/auth")
    @ApiOperation(value = "MQTT认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientid",value="clientid",required = true,dataType = "String"),
            @ApiImplicitParam(name = "username",value = "账号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "password",value = "密码", required = true,dataType ="String")
    })
    public void accountAuth(String clientid, String username, String password, HttpServletResponse response){
        if(mqttAuthService.auth(clientid,username, password)){
            response.setStatus(CommonConstant.SC_OK_200);
        }else{
            response.setStatus(CommonConstant.SC_AUTH_FAILED);
        }
    }

    @GetMapping("/acl")
    @ApiOperation(value = "MQTT ACL认证")
    public void aclAuth(String access,String username,String clientid,String ipaddr,String topic,String mountpoint,HttpServletResponse response){
        if(mqttAuthService.aclAuth(access,username, clientid,ipaddr, topic,mountpoint)){
            response.setStatus(CommonConstant.SC_OK_200);
        }else{
            response.setStatus(CommonConstant.SC_AUTH_FAILED);
        }
    }

    @PostMapping("/superuser")
    @ApiOperation(value = "MQTT superUser认证")
    public void isSuperUser(String clientid,String username,HttpServletResponse response){
        if(mqttAuthService.isSuperUser(clientid,username)){
            response.setStatus(CommonConstant.SC_OK_200);
        }else{
            response.setStatus(CommonConstant.SC_AUTH_FAILED);
        }
    }
}
